//
//  TJEmotion.swift
//  表情键盘
//
//  Created by 唐后能 on 16/4/21.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class TJEmotion: NSObject,NSCoding {

    // 表情包文件夹名称
    var id :String?
    
    // chs.图片传输的名称
    var chs:String?
    
    // png图片名称.->用来显示,因为不在mainBundel下,需要全路径.
    var png:String? {
    
        didSet{
        
            fullPngPath = bundlePath! + "/" + id! + "/" + png!
        }
    }
    
    var fullPngPath:String?
    
    // code-->16进制字符串,要转成emoji才能使用.
    var code:String? {
    
        didSet {
        
            // KVC设置code后,将code转成emoji
            // 扫描 String -> Int
            let scanner = NSScanner(string: code!)
            
            var result: UInt32 = 0
            // result: UnsafeMutablePointer<UInt32>: 需要一个UInt32类型的可变指针
            scanner.scanHexInt(&result)
            
            let scalar = UnicodeScalar(result)
            let c = Character(scalar)
            emoji = String(c)
        }
    }
    
    // emoji->设置给textView之后就可以直接显示表情.
    var emoji:String?
    
    // 字典转模型.
    init(dict:[String:String],id:String) {
        self.id = id
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    // 给模型中没有的key赋空值
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    // 打印对象.
    override var description:String {
    
        get {
        
            let keys = ["chs","png","code","id","emoji","fullPngPath"]
            return "Emotion模型:\(dictionaryWithValuesForKeys(keys).description)"
        }
    }
    
    
    // 表情模型的归档.
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(chs, forKey: "chs")
        aCoder.encodeObject(png, forKey: "png")
        aCoder.encodeObject(code, forKey: "code")
        aCoder.encodeObject(id, forKey: "id")
        aCoder.encodeObject(emoji, forKey: "emoji")
    }
    // 表情模型的解挡.不会走disSet方法了.
    required init?(coder aDecoder: NSCoder) {
        
        self.chs = aDecoder.decodeObjectForKey("chs") as? String
        self.png = aDecoder.decodeObjectForKey("png") as? String
        self.code = aDecoder.decodeObjectForKey("code") as? String
        self.id = aDecoder.decodeObjectForKey("id") as? String
        self.emoji = aDecoder.decodeObjectForKey("emoji") as? String
        super.init()
        if self.png != nil {
        
            fullPngPath = bundlePath! + "/" + id! + "/" + png!
        }
    }
    
    /// 将emoticon表情模型转成带图片附件的属性文本
    func emotionModelToAttrString(font: UIFont) -> NSAttributedString {
        // 图片->附件->属性文本
        let atachment = TJTextAttachment()
        atachment.image = UIImage(named: fullPngPath!)
        // 设置表情名称给附件
        atachment.chs = chs
        
        let wh = (font.lineHeight ?? 20)// * 0.8
        // bounds和frame正好相反,往下移是负数
        atachment.bounds = CGRect(x: 0, y: -3, width: wh, height: wh)
        
        // 转成可变的属性文本
        let attrM = NSMutableAttributedString(attributedString: NSAttributedString(attachment: atachment))
        
        // 给附件设置UIFont属性,不然后面的表情会很小
        attrM.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        
        return attrM
    }
    
    /// 用表情名称去找模型
    class func emoticonNameToEmoticonModel(name: String) -> TJEmotion? {
        // 表情包里面有模型
        // 遍历每个表情包
        for packageModel in TJEmotionManager.shareEmotionManager.emotionPackages {
            // 表情包里面有多个表情模型
            for emoticon in packageModel.emoticons {
                // 查找表情名称一样的
                if emoticon.chs == name {
                    // 找到了
                    return emoticon
                }
            }
        }
        
        return nil
    }

    
}
